package com.heitaoking.blog.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import com.heitaoking.blog.entity.Blog;
import com.heitaoking.blog.util.JdbcUtil;

public class BlogDao {

	public List<Blog> queryBlog(Blog blog) throws Exception {
		// 获取connection对象
		Connection con = JdbcUtil.getConnection();
		// 通过connection对象获得"预编译语句"对象
		String sql = "select * from tbl_blogname";
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);
		ArrayList<Blog> blogList = new ArrayList<Blog>();

		while (rs.next()) {
			Blog blog2 = new Blog();
			blog2.setBiaotiname(rs.getString("title"));
			blog2.setBiaotiname2(rs.getString("content"));
			blogList.add(blog2);
		}
		// 释放数据库连接资源(切勿忘记)
		JdbcUtil.release();
		return blogList;
	}

	public void insertBlog(Blog blog) throws Exception {
		// 获取connection对象
		Connection con = JdbcUtil.getConnection();
		// 通过connection对象获得"预编译语句"对象
		String sql = "insert into TBL_BLOGNAME (bid,title,content,usid,bcreatetime) values(seq_bid.nextval,?,?,?,sysdate)";
		PreparedStatement ps = con.prepareStatement(sql);
		// 设置"预编译语句"对象中的参数(从1开始计数)
		ps.setString(1, blog.getBiaotiname());
		ps.setString(2, blog.getBiaotiname2());
		ps.setString(3, blog.getUsid());
	
		// 执行sql操作
		ps.executeUpdate();
		// 释放数据库连接资源(切勿忘记)
		JdbcUtil.release();

	}

}
